package cqrtplm.controller;

import cn.hutool.core.collection.CollUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.github.pagehelper.PageInfo;
import com.hustcad.plm.pdm.file.service.TyplmFileHandleService;
import com.ty.basic.entity.IdentifierEntity;
import com.ty.basic.handler.exceptionhandler.exception.ResponseCodeEnum;
import com.ty.basic.handler.exceptionhandler.exception.TyException;
import com.ty.basic.response.ResponseResult;
import cqrtplm.constant.RouteConstant;
import cqrtplm.dto.query.DspfStatusBaseQueryDTO;
import cqrtplm.service.IDspfStatusBaseService;
import cqrtplm.service.RTChangeService;
import cqrtplm.vo.DspfStatusBaseVO;
import cqrtplm.vo.EbomInfoVO;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 状态基础Controller
 *
 * @author 江小雄
 * @date 2025-07-11
 */
@RestController
@RequestMapping(RouteConstant.DSPFSTATUSBASE + "v1")
public class DspfStatusBaseController
{
    @Autowired
    private IDspfStatusBaseService dspfStatusBaseService;
    @Resource
    private TyplmFileHandleService typlmFileHandleService;


    @PostMapping("/list")
    public PageInfo<DspfStatusBaseVO> list(@RequestBody DspfStatusBaseQueryDTO queryDTO) {
        return dspfStatusBaseService.selectPageList(queryDTO, queryDTO.getPageNum()==null ? 1: queryDTO.getPageNum(), queryDTO.getPageSize()==null ? 10: queryDTO.getPageSize());
    }

    /**
     * 根据开发令机型查询EBOM列表
     * @param queryDTO
     * @return
     */
    @PostMapping("/pageEbomList")
    public PageInfo<DspfStatusBaseVO> pageEbomList(@RequestBody DspfStatusBaseQueryDTO queryDTO)
            throws JsonProcessingException {
        PageInfo<DspfStatusBaseVO> pageInfo = dspfStatusBaseService.queryByConditionForPage(queryDTO);
        return pageInfo;
    }

    /**
     * 获取EBOM详情
     * @param queryDTO
     * @return
     */
    @PostMapping("/getEbomInfo")
    public PageInfo<EbomInfoVO> getEbomInfo(@RequestBody DspfStatusBaseQueryDTO queryDTO) throws JsonProcessingException{
        PageInfo<EbomInfoVO> pageInfo = dspfStatusBaseService.getEbomInfo(queryDTO);
        return pageInfo;
    }

    /**
     * 文件上传
     * @param files
     * @return
     */
    @PostMapping("/simpleUploadFile")
    public ResponseResult simpleUploadFile(List<MultipartFile> files){
        List<ImmutablePair<String, IdentifierEntity>> list = typlmFileHandleService.batchSimpleUploadFile(files);
        if (CollUtil.size(list) != CollUtil.size(files)) {
            TyException.throwSingleTyExceptionVarargs(ResponseCodeEnum.BIZ_UPLOAD_FILE_FAILD);
        }
        List<IdentifierEntity> entityList = list.stream().map(ImmutablePair::getRight).collect(Collectors.toList());
        List<String> oidStringList = entityList.stream()
                .map(entity -> entity.getOid() != null ? entity.getOid().toString() : "0")
                .collect(Collectors.toList());
        return ResponseResult.success(oidStringList);
    }


//    /**
//     * 查询状态基础列表
//     */
//    @PreAuthorize("@ss.hasPermi('system:base:list')")
//    @GetMapping("/list")
//    public TableDataInfo list(DspfStatusBase dspfStatusBase)
//    {
//        startPage();
//        List<DspfStatusBase> list = dspfStatusBaseService.selectDspfStatusBaseList(dspfStatusBase);
//        return getDataTable(list);
//    }
//
//    /**
//     * 导出状态基础列表
//     */
//    @PreAuthorize("@ss.hasPermi('system:base:export')")
//    @Log(title = "状态基础", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, DspfStatusBase dspfStatusBase)
//    {
//        List<DspfStatusBase> list = dspfStatusBaseService.selectDspfStatusBaseList(dspfStatusBase);
//        ExcelUtil<DspfStatusBase> util = new ExcelUtil<DspfStatusBase>(DspfStatusBase.class);
//        util.exportExcel(response, list, "状态基础数据");
//    }
//
//    /**
//     * 获取状态基础详细信息
//     */
//    @PreAuthorize("@ss.hasPermi('system:base:query')")
//    @GetMapping(value = "/{statusid}")
//    public AjaxResult getInfo(@PathVariable("statusid") String statusid)
//    {
//        return success(dspfStatusBaseService.selectDspfStatusBaseByStatusid(statusid));
//    }
//
//    /**
//     * 新增状态基础
//     */
//    @PreAuthorize("@ss.hasPermi('system:base:add')")
//    @Log(title = "状态基础", businessType = BusinessType.INSERT)
//    @PostMapping
//    public AjaxResult add(@RequestBody DspfStatusBase dspfStatusBase)
//    {
//        return toAjax(dspfStatusBaseService.insertDspfStatusBase(dspfStatusBase));
//    }
//
//    /**
//     * 修改状态基础
//     */
//    @PreAuthorize("@ss.hasPermi('system:base:edit')")
//    @Log(title = "状态基础", businessType = BusinessType.UPDATE)
//    @PutMapping
//    public AjaxResult edit(@RequestBody DspfStatusBase dspfStatusBase)
//    {
//        return toAjax(dspfStatusBaseService.updateDspfStatusBase(dspfStatusBase));
//    }
//
//    /**
//     * 删除状态基础
//     */
//    @PreAuthorize("@ss.hasPermi('system:base:remove')")
//    @Log(title = "状态基础", businessType = BusinessType.DELETE)
//	@DeleteMapping("/{statusids}")
//    public AjaxResult remove(@PathVariable String[] statusids)
//    {
//        return toAjax(dspfStatusBaseService.deleteDspfStatusBaseByStatusids(statusids));
//    }
}
